﻿@{

    var gen = Model;
}

using @(gen.NameSpace).Application.Services.interfaces;
using @(gen.NameSpace).Domain.Entities;
using @(gen.NameSpace).Domain.Services;
using @(gen.NameSpace).Application.Services.Iml;
using @(gen.NameSpace).Shared.Dtos.Out;
using @(gen.NameSpace).Shared.Dtos.Input;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow;

namespace @(gen.NameSpace).Application.Services
{
    [Authorize]
    public class @(gen.TableName)Service: BaseService, I@(gen.TableName)Service
    {
        I@(gen.TableName)ManagerService _manager;
        public @(gen.TableName)Service(I@(gen.TableName)ManagerService manager,
            IObjectMapper<@(gen.NameSpace)Module> mapper,
            IGlobalManagerService globalManager):base(mapper,globalManager)
        {
            _manager = manager;
        }

        public Result<@(gen.TableName)SearchOut> @(gen.TableName)s(@(gen.TableName)Search search)
        {
            var result = _manager.All(search.Index, search.PageSize, search.Key);
            var list = _mapper.Map<List<@(gen.TableName)>, List<@(gen.TableName)Out>>(result);
            var count = _manager.Count(search.Key);
            return Ok(new @(gen.TableName)SearchOut { Items = list, TotalCount = count });
        }

        public Result RealDelete(Guid id)
        {
            if (_globalManager.RealDelete<@(gen.TableName)>(id))
                return Ok();
            else
                return Error("删除失败!");
        }

        public Result<@(gen.TableName)Out> Get(Guid id)
        {
            var item = _globalManager.Get<@(gen.TableName)>(id);
            var result = _mapper.Map<@(gen.TableName), @(gen.TableName)Out>(item);
            return Ok(result);
        }

        public Result Edit(@(gen.TableName)Out @(gen.LowerTableName))
        {
            bool result;
            var model = _mapper.Map<@(gen.TableName)Out, @(gen.TableName)>(@(gen.LowerTableName));
            if (@(gen.LowerTableName).Id == null)
            {
                model.Id = Guid.NewGuid();
                result = _globalManager.Insert(model);
            }
            else
            {
                result = _globalManager.Update(model);
            }
            if (result)
                return Ok();
            else
                return Error("保存失败!");
        }

        [HttpPost]
        public Result Delete(Guid[] ids)
        {
            if (ids?.Length < 1)
            {
                return Error("请选择");
            }
            var result = _globalManager.DeleteByWhere<@(gen.TableName)>(p => ids!.Contains(p.Id));
            if (result)
                return Ok();
            else
                return Error("删除失败!");
        }
    }
}
